#include "tablemodel.h"

#include <QChar>
#include <assert.h>


DPOTableModel::DPOTableModel(QObject *parent)
    : QAbstractTableModel(parent), 
	_columnCount(0), 
	_LoadFilter(0)
{
}

DPOTableModel::DPOTableModel(const QString & fileName, QObject *parent)
    : QAbstractTableModel(parent), 
	_columnCount(0),
	_LoadFilter(0)
{
	load(fileName);
}

void DPOTableModel::reset(ViewMode vm)
{
	QAbstractTableModel::reset();
}

void DPOTableModel::AddData( const char * data, unsigned int size)
{
	_data.back().ba = QByteArray(data, size);
}

bool DPOTableModel::load( const QString & fileName, int splitsize /*= -1*/ )
{
	clear();

	_fileName = fileName;

	_file.setFileName(fileName);
	if (false == _file.open(QIODevice::ReadOnly) ) return false;

	//if (_LoadFilter)
	//{
	//	_LoadFilter->startLoad(this);
	//}

	while (!_file.atEnd())
	{
		QByteArray ba;
		unsigned int sz = 0;
		if (splitsize == -1)
		{
			ba = _file.read(2);
			sz = *(short*)ba.constData();//ba.toShort(&ok);
			if (0 == sz) continue;
		}
		else
		{
			sz = splitsize;
		}

		DataItem da;
		da.pos = _file.pos();
		da.size = sz;

		//ba = _file.read(sz);
		//if (ba.size() < sz) 
		//{
		//	sz = ba.size();
		//}

		//if (_LoadFilter)
		//{
		//	if (false == _LoadFilter->loadThisData(ba.data(), ba.size()))
		//		continue;
		//}
		_data.push_back(da);
		bool rc = _file.seek(_file.pos() +  sz);
		assert(rc);
		if (sz > _columnCount) _columnCount = sz;
		//AddData(ba.data(), ba.size());
	}

	if (_LoadFilter)
	{
		_LoadFilter->finishLoad();
	}

	QAbstractTableModel::reset();
	return true;
}

QString DPOTableModel::getFilename() const
{
	return _fileName;
}

void DPOTableModel::clear()
{
	_data.clear();
	_columnCount = 0;
	_fileName.clear();
	_file.close();
}


int DPOTableModel::rowCount(const QModelIndex &parent) const
{
	if (parent.isValid()) return 0;
    return _data.size();
}

int DPOTableModel::columnCount(const QModelIndex &parent) const
{
	if (parent.isValid()) return 0;
    return _columnCount;
}

QVariant DPOTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();
    
    if (index.row() >= _data.size() || index.row() < 0) return QVariant();
    
	DataItem & dataitem = _data[index.row()];
	if (dataitem.size != dataitem.ba.size())
	{
		if (!_file.seek(dataitem.pos)) { assert(false);}

		dataitem.ba = _file.read(dataitem.size);
		if (dataitem.ba.size() != dataitem.size)
		{
			if (_file.atEnd())
				dataitem.size = dataitem.ba.size();
			else { assert(false);}
		}
	}

	if (dataitem.ba.size() <= index.column()) return QVariant();

    if (role == Qt::DisplayRole) {
		return dataitem.ba.at(index.column());
	}

	if (role == Qt::TextAlignmentRole){
		return (int)(Qt::AlignCenter | Qt::AlignVCenter);
	}

    return QVariant();
}

QVariant DPOTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	//return QString::number(section + 1);

	if (role != Qt::DisplayRole)
        return QVariant();
    
    if (orientation == Qt::Horizontal){
		return QString::number(section + 1);
    } else if ( orientation == Qt::Vertical )
	{
		return QString("%1 : %2").arg(QString::number(section + 1)).arg(_data.at(section).size);
	}

    return QVariant();
}

void DPOTableModel::setLoadFilter( ILoadFilter * lf)
{
	_LoadFilter = lf;
}

//Qt::ItemFlags DPOTableModel::flags(const QModelIndex &index) const
//{
//    //if (!index.isValid())
//    //    return Qt::ItemIsEnabled;
//    
//    return Qt::ItemIsSelectable ;//QAbstractTableModel::flags(index);
//}
